#include "machine_setting.h"
#include <QDebug>

MACHINE_SETTING::MACHINE_SETTING()
{
    //配置文件的名称为label_print_desktop.ini
    //配置文件的位置在C:\Users\用户名\AppData\Roaming
    setting = new QSettings(QSettings::IniFormat,QSettings::UserScope,"label_print_desktop");
    read_setting();
}


void MACHINE_SETTING::save_setting()
{    if(!setting->contains("build_flag"))
    {
        setting->setValue("build_flag","check");
    }
    //一般设置
    setting->setValue("NORMAL/label_width",NORMAL.label_width);
    setting->setValue("NORMAL/label_height",NORMAL.label_height);
    setting->setValue("NORMAL/label_offset_width",NORMAL.label_offset_width);
    setting->setValue("NORMAL/label_offset_height",NORMAL.label_offset_height);
    setting->setValue("NORMAL/Machine_Driver",NORMAL.Machine_driver);
    setting->setValue("NORMAL/Code_type",NORMAL.Code_type);
    setting->setValue("NORMAL/line_distance",NORMAL.line_distance);
    //一维码设置
    setting->setValue("BARCODE/barcode_A",BARCODE.barcode_A);
    setting->setValue("BARCODE/barcode_B",BARCODE.barcode_B);
    setting->setValue("BARCODE/barcode_turn",BARCODE.barcode_turn);
    setting->setValue("BARCODE/barcode_type",BARCODE.barcode_type);
    setting->setValue("BARCODE/barcode_human",BARCODE.barcode_human);
    setting->setValue("BARCODE/barcode_height",BARCODE.barcode_height);
    //二维码设置
    setting->setValue("QRCODE/qrcode_cell",QRCODE.qrcode_cell);
    setting->setValue("QRCODE/qrcode_ecc",QRCODE.qrcode_ecc);
    setting->setValue("QRCODE/qrcode_mask",QRCODE.qrcode_mask);
    setting->setValue("QRCODE/qrcode_mode",QRCODE.qrcode_mode);
    setting->setValue("QRCODE/qrcode_turn",QRCODE.qrcode_turn);
    setting->setValue("QRCODE/qrcode_model",QRCODE.qrcode_model);
    //文本设置
    setting->setValue("TEXT/text_horizontal",TEXT.text_horizontal);
    setting->setValue("TEXT/text_vertical",TEXT.text_vertical);
    setting->setValue("TEXT/text_turn",TEXT.text_turn);
    setting->setValue("TEXT/text_type",TEXT.text_type);
}
void MACHINE_SETTING::read_setting()
{
    //通过构建标志来检查文件有没有经过初始化，确保初始化后再进行读取
    if(!setting->contains("build_flag"))
    {
        init_setting();
    }

    //一般设置
    NORMAL.label_width = setting->value("NORMAL/label_width").toInt();
    NORMAL.label_height = setting->value("NORMAL/label_height").toInt();
    NORMAL.label_offset_height = setting->value("NORMAL/label_offset_height").toInt();
    NORMAL.label_offset_width = setting->value("NORMAL/label_offset_width").toInt();
    NORMAL.Machine_driver = setting->value("NORMAL/Machine_Driver").toString();
    NORMAL.Code_type = setting->value("NORMAL/Code_type").toString();
    NORMAL.line_distance = setting->value("NORMAL/line_distance").toInt();
    //一维码
    BARCODE.barcode_A = setting->value("BARCODE/barcode_A").toInt();
    BARCODE.barcode_B = setting->value("BARCODE/barcode_B").toInt();
    BARCODE.barcode_height = setting->value("BARCODE/barcode_height").toInt();
    BARCODE.barcode_human = setting->value("BARCODE/barcode_human").toInt();
    BARCODE.barcode_turn = setting->value("BARCODE/barcode_turn").toInt();
    BARCODE.barcode_type = setting->value("BARCODE/barcode_type").toString();
    //二维码
    QRCODE.qrcode_cell = setting->value("QRCODE/qrcode_cell").toInt();
    QRCODE.qrcode_ecc = setting->value("QRCODE/qrcode_ecc").toString();
    QRCODE.qrcode_mask = setting->value("QRCODE/qrcode_mask").toInt();
    QRCODE.qrcode_mode = setting->value("QRCODE/qrcode_mode").toString();
    QRCODE.qrcode_turn = setting->value("QRCODE/qrcode_turn").toInt();
    QRCODE.qrcode_model = setting->value("QRCODE/qrcode_model").toInt();
    //文本
    TEXT.text_horizontal = setting->value("TEXT/text_horizontal").toInt();
    TEXT.text_vertical = setting->value("TEXT/text_vertical").toInt();
    TEXT.text_turn = setting->value("TEXT/text_turn").toInt();
    TEXT.text_type = setting->value("TEXT/text_type").toString();
}

void MACHINE_SETTING::init_setting()
{
    //一般设置
    setting->setValue("NORMAL/label_width","40");
    setting->setValue("NORMAL/label_height","60");
    setting->setValue("NORMAL/label_offset_width","3");
    setting->setValue("NORMAL/label_offset_height","3");
    setting->setValue("NORMAL/Machine_Driver","Gprinter GP-3120TL");
    setting->setValue("NORMAL/Code_type","txt");
    setting->setValue("NORMAL/line_distance","25");
    //一维码设置
    setting->setValue("BARCODE/barcode_A","1");
    setting->setValue("BARCODE/barcode_B","1");
    setting->setValue("BARCODE/barcode_turn","0");
    setting->setValue("BARCODE/barcode_type","128");
    setting->setValue("BARCODE/barcode_human","1");
    setting->setValue("BARCODE/barcode_height","50");
    //二维码设置
    setting->setValue("QRCODE/qrcode_cell","3");
    setting->setValue("QRCODE/qrcode_ecc","H");
    setting->setValue("QRCODE/qrcode_mask","7");
    setting->setValue("QRCODE/qrcode_mode","A");
    setting->setValue("QRCODE/qrcode_turn","0");
    setting->setValue("QRCODE/qrcode_model","1");
    //文本设置
    setting->setValue("TEXT/text_horizontal","1");
    setting->setValue("TEXT/text_vertical","1");
    setting->setValue("TEXT/text_turn","0");
    setting->setValue("TEXT/text_type","TSS24.BF2");

    //写入构建标记
    setting->setValue("build_flag","check");
}

setting_normal& MACHINE_SETTING::get_NORMAL()//获得NORMAL设置
{
    return NORMAL;
}
setting_barcode& MACHINE_SETTING::get_BARCODE()//获得BARCODE设置
{
    return BARCODE;
}
setting_qrcode& MACHINE_SETTING::get_QRCODE()//获得QRCODE设置
{
    return QRCODE;
}
setting_text& MACHINE_SETTING::get_TEXT()//获得TEXT设置
{
    return TEXT;
}
